#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/loadavg.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>

#define err() do { perror("server3"); exit(EXIT_FAILURE); } while(0)

int main(int argc, char *argv[])
{
	pid_t pid;
	uid_t uid;
	gid_t gid;
	int fid;
	char * data;
	double load[3];
	time_t start;
	pid = getpid();
	uid = getuid();
	gid = getgid();
	start = time(NULL);
	if(( fid = open("shared", O_RDWR|O_CREAT, 0666)) < 0 ) err();
	if((data = (char*)mmap(NULL, 1024, PROT_WRITE, MAP_SHARED, fid, 0)) == MAP_FAILED) err();
	for(;;)
	{
		if(getloadavg(load,3) == -1) err();
		sprintf(data,"pid:%ld\nuid:%ld\ngid:%ld\nseconds:%ld\n1:%f\t5:%f\t15:%f\n",pid,uid,gid,time(NULL)-start,load[0],load[1],load[2]);
		sleep(1);
	}
	return 0;
}
